import WxValidate from "../../../utils/wx-validate/WxValidate";
import Promise from '../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../utils/regenerator-runtime/runtime'
const utils = require('../../../utils/util.js');
const app=getApp();
Page({
  data: {
    iconSize: [20],
    iconColor: ['#f00'],
    iconType: ['cancel'],
    token:null,
    headImg:null,
    nickname:null,
    form: {
      uname:"",
      mobile:""/* ,
      company_nature:"",
      is_social_security: "",
      is_accumulation_fund: "",
      is_bank_card: "",
      bank_num: "",
      apply_quota: "" */
    },
    /* jobCompany: ["政府单位","国企事业单位","世界500强","上市公司","其他"],
    bankcardInfo: ["否", "是"],
    fundInfo: ["否", "是"],
    socialInfo: ["否", "是"],
    bankNumInfo: ["0","1","2","3","4","5","6","7","8","9","10"], */
    url:"/api/users/getInfo",
    formUrl:"/api/users/upusers"
  },
  onLoad: async function() {
    if(app.globalData.userInfo){
      this.setData({
        nickname:app.globalData.userInfo.nickname,
        headImg:app.globalData.userInfo.headimg
      })
    }
    else{
      wx.redirectTo({
        url: './pages/index/index'
      })
      return;
    }
    this.initValidate();
    console.log("onLoad");
    const token = await app.getToken();
    this.setData({
      token:token
    })
    var data=await this._getInfo();
    if(data.state){
      var info=data.data;
      this.setData({
        "form.uname":info.uname,
        "form.user_id":info.user_id,
        "form.mobile":info.mobile
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      return;
    }
    //console.log(this.WxValidate);
  },
  _getInfo:function(){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.url,{token:this.data.token},res=>{
          //console.log(res);
          var data=res.data;
          if(data.state){
            resolve({state:true,data:data.data});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"获取失败，请重试"});
        })
    });
    return p;
  },
  initValidate() {
    // 验证字段的规则
    const rules = {
      uname: {
        required: true,
        truename: true
      },
      mobile: {
        required: true,
        tel:true
      }/* ,
      company_nature: {
        required: true
      },
      is_social_security: {
        required: true
      },
      is_accumulation_fund: {
        required: true
      },
      is_bank_card: {
        required: true
      },
      bank_num: {
        required: true
      },
      apply_quota: {
        required: true
      } */
    };

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      uname: {
        required: "请输入姓名",
        truename: "姓名格式不正确"
      },
      mobile: {
        required: "请输入手机号码",
        tel:"手机号码格式不正确"
      }/* ,
      company_nature: {
        required: "请选择工作单位性质"
      },
      is_social_security: {
        required: "请选择社保缴纳情况"
      },
      is_accumulation_fund: {
        required: "请选择公积金缴纳情况"
      },
      is_bank_card: {
        required: "请选择是否已有该行信用卡"
      },
      bank_num: {
        required: "请选择已持有信用卡行数"
      },
      apply_quota: {
        required: "请填写信用卡授信额度"
      } */
    };
    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages);
  },
  submitForm(e) {
    const params = e.detail.value;
    params.formId = e.detail.formId;
    console.log(params);
    // 传入表单数据，调用验证方法
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0];
      this.showModal(error);
      return false;
    }
    wx.showLoading({
      title:"提交中，请稍后..."
    })
    //debugger;
    params.token=this.data.token;
    //params.bankid=this.data.form.bankid;
    console.log(params);
    app.request(this.data.formUrl,params,res=>{
      wx.hideLoading();
      var data=res.data;
      if(data.state){
        this.showModal(data,function(){
          wx.navigateBack({
            delta: 1
          })
        })
      }
      else{
        this.showModal(data);
      }
    },()=>{
      wx.hideLoading();
      this.showModal('提交失败，请重试');
    })
  },
  showModal(error,cb) {
    wx.showModal({
      content: error.msg || error,
      showCancel: false,
      success(res) {
        if(cb) cb();
      }
    });
  },
  pickerChange(e) {
    var name=e.target.dataset.name;
    var key = "form." + name;
    this.setData({
      [key]: e.detail.value
    });
  }
});
